﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;

namespace DNA.Mvc.Installation
{
    /// <summary>
    /// Defines the Installer base class
    /// </summary>
    public abstract class InstallerBase : Command<InstallationContext>
    {
        /// <summary>
        /// Gets the installer name
        /// </summary>
        public abstract string Name { get; }

        //public virtual HttpContextBase HttpContext { get; set; }

        /// <summary>
        /// Gets/Sets the current file path that contains installation files.
        /// </summary>
        public string ExecutionPath { get; set; }

        /// <summary>
        /// Gets/Set the installer configuation xml.
        /// </summary>
        public string ConfigXml { get; set; }

        /// <summary>
        /// Load configXml and returns the XmlDocument object.
        /// </summary>
        /// <returns>If ConfigXml is empty returns null.</returns>
        public XmlDocument GetConfiguationDocument()
        {
            if (!string.IsNullOrEmpty(ConfigXml))
            {
                try
                {
                    var doc = new XmlDocument();
                    doc.LoadXml(ConfigXml);
                }
                catch { return null; }
            }
            return null;
        }

        /// <summary>
        /// Get the strongly type configuation object from confg xml.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetConfig<T>()
        {
            if (!string.IsNullOrEmpty(ConfigXml))
                return DNA.Utility.XmlSerializerUtility.DeserializeFromXmlText<T>(ConfigXml);
            return default(T);
        }
    }
}
